import React,{Component,PropTypes} from 'react';
import { Form, Input, Button, Checkbox, notification,message} from 'antd';

const FormItem = Form.Item;
const createForm = Form.create;

import {login} from '../services/login';

import './less/login.less';

class Login extends Component {
  constructor(props) {
    super(props);
    this.state = {
	  	username:void 0,
	  	password:void 0
	  };
    this.handleSubmit = this.handleSubmit.bind(this);
  }

  handleSubmit(e) {
    e.preventDefault();

    this.props.form.validateFields((errors, values) => {
      if (!!errors) {
          return;
      }else{
          var options = {sleep:true ,resultType: "json", xml: '<?xml version=\"1.0\" encoding=\"utf-8\" ?><ajaxStorage><action><![CDATA[loginCheck]]></action><loginName><![CDATA[gj036]]></loginName><password><![CDATA[1]]></password></ajaxStorage>' };
          login({
              method: 'POST',
              body:"sleep=true&resultType=json&xml=<?xml version=\"1.0\" encoding=\"utf-8\" ?><ajaxStorage><action><![CDATA[loginCheck]]></action><loginName><![CDATA[gj036]]></loginName><password><![CDATA[gj1903]]></password></ajaxStorage>"
          }).then(data => {
            if(data.message.returnCode === 0){
                this.context.router.push('/app/index');
            }else{
                message.error('信息错误');
            }
            //this.context.router.push('/app/index');
          });
      }
    });
  }

  render() {
    const { getFieldProps, getFieldError, isFieldValidating } = this.props.form;

    const nameProps = getFieldProps('name', {
      rules: [
        { required: true, min: 5, message: '用户名至少为 5 个字符' },
      ],
    });
    const passwdProps = getFieldProps('passwd', {
      rules: [
        { required: true, whitespace: true, message: '请填写密码' },
        { validator: this.checkPass },
      ],
    });
    return (
      <div className="login-container">
        <div className="login-mask"></div>
        <Form className="login-content" horizontal onSubmit={this.handleSubmit}>
          <h2><img src={require('../assets/logo.png')} />冠江OA</h2>
          <div className="subTitle">登录云台后台管理平台</div>
          <FormItem label="账号" required ={false}>
          	<Input placeholder="请输入账户名" {...nameProps}/>
          </FormItem>
          <FormItem label="密码" required ={false}>
            <Input type="password" placeholder="请输入密码" {...passwdProps}/>
          </FormItem>
          <FormItem>
	          <Checkbox style={{color:'#FFF'}}>记住我</Checkbox>
	       </FormItem>
          <FormItem>
            <Button className="ant-col-24" type="primary" htmlType="submit">登录</Button>
          </FormItem>
        </Form>
      </div>
    );
  }
}

Login.contextTypes = {
    router: React.PropTypes.object.isRequired
}

Login = createForm()(Login);

export default Login;
